package com.bistu.openlab.uofspreadsheet.xmlparse;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.dom4j.Document;
import org.dom4j.Element;

import com.bistu.openlab.uofspreadsheet.model.ExcelExtend;
import com.bistu.openlab.uofspreadsheet.util.XmlDomHelper;

/*
 *@author xiaoguang.gao@oracle.com
 *@version 2013-5-8 
 */
public class ExcelExtendParse implements IXmlParse<ExcelExtend> {
	private static ExcelExtendParse parse = new ExcelExtendParse();
	public static ExcelExtendParse getInstance(){
		return parse;
	}
	private ExcelExtendParse(){}
	
	@Override
	public ExcelExtend readFromElement(Element root) {
		ExcelExtend extend = null;
		if(root!=null){
			extend = new ExcelExtend();
			extend.setRoot((Element)root.clone());
		}
		return extend;
	}
	@Override
	public Element writeToElement(ExcelExtend obj) {
		Element root = null;
		if(obj==null||obj.getRoot()==null){
			URL url = ClassLoader.getSystemClassLoader().getResource("com/bistu/openlab/uofspreadsheet/resource/template/extend.xml");
			try {
				URLConnection connection = url.openConnection();
				InputStream stream = connection.getInputStream();
				Document doc = XmlDomHelper.GetDocFromInputStream(stream);
				if(doc!=null)
					root = (Element)doc.getRootElement().clone();
			}catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			root = obj.getRoot();
		}
		return root;
	}

}
